package com.wjm.demo.test0.io_test;

import com.wjm.demo.test0.io_test.entity.Hero;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class IODemo {

    public static void main(String[] args) throws IOException {

//        fileWriterTest();  //字符输出流
//        bufferedWriterTest();  //字符缓冲输出流
//        bufferedWriterTest2();  //字符缓冲输出流 写集合中的数据
//        fileReaderTest();  //字符输入流
//        bufferedReaderTest();  //字符缓冲输入流
//        bufferedReaderTest2();  //字符缓冲输入流


        //5个游戏案例
        //首先写框架代码，再填功能
        LOL();


    }

    /**
     * 5个游戏案例
     */
    private static void LOL() throws IOException {
        //1.首先创建一个集合，用来存储五个英雄
        List<Hero> list = new ArrayList<>();

        //2。读取文件，把数据加到集合中
//        System.out.println("加载文件的数据到集合中 TODO");
        loadFileToList(list);

        //3.判断一下集合当中有没有内容，是不是新的？
        boolean isNew = list.size() == 0;
        System.out.println("是不是新队伍：" + (isNew ? "是" : "不是"));

        //如果新的队伍，没有人，就需要创建五个对象加入到集合中
        if (isNew) {
            System.out.println("创建5个对象加放集合中");
            addFiveHeros(list);
        }
        //如果队伍不是新的，已经有人了，就不需要创建了。
        System.out.println("我们队伍的阵容是");
        //遍历集合，输出其中每一个对象的具体信息
//        System.out.println("循环5次遍历");
        showHeros(list);

        //统计一下总共战斗力是多少
        int totalAttack = getTotalAttack(list);
        System.out.println("我们队伍的总战斗力是：" + totalAttack);

        //判断一下集合是不是新的，是就存储到文件中
        if (isNew) {
//            System.out.println("将集合数据写到文件中");
            saveToFile(list);
        }
        //如果集合不是新的，而是从文件中加载得到的数据，就什么都不做
        System.out.println("退出游戏");
    }

    private static void loadFileToList(List<Hero> list) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader("file1.txt"));
        String line = null;
        while ((line = br.readLine()) != null) {
            String[] split = line.split(",");
            Hero hero = new Hero(split[0], Integer.parseInt(split[1]), split[2]);
            list.add(hero);
        }
    }

    private static void saveToFile(List<Hero> list) throws IOException {
        BufferedWriter bw = new BufferedWriter(new FileWriter("file1.txt"));
        for (int i = 0; i < list.size(); i++) {
            Hero hero = list.get(i);
            String str = hero.getName() + "," + hero.getAttack() + "," + hero.getType();
            bw.write(str);
            bw.newLine();
        }
        bw.close();
    }

    private static int getTotalAttack(List<Hero> list) {
        int attackAll = 0;
        for (int i = 0; i < list.size(); i++) {
            Hero hero = list.get(i);
            attackAll += hero.getAttack();
        }
        return attackAll;
    }

    private static void showHeros(List<Hero> list) {
        for (int i = 0; i < list.size(); i++) {
            Hero hero = list.get(i);
            System.out.println("英雄的名字：" + hero.getName() + "战斗力：" + hero.getAttack() + "类型：" + hero.getType());
        }
    }

    private static void addFiveHeros(List<Hero> list) {
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < 5; i++) {
            System.out.println("请输入英雄名");
            String name = sc.next();
            System.out.println("请输入英雄战斗力");
            int attack = sc.nextInt();
            System.out.println("请输入英雄类型");
            String type = sc.next();
            Hero hero = new Hero(name, attack, type);
            list.add(hero);
        }
    }

    private static void bufferedWriterTest2() throws IOException {
        //字符缓冲输出流
        List<String> list = new ArrayList<>();
        list.add("中");
        list.add("国");
        list.add("人");
        //缓冲流里面有一个8192长度的char[]字符数组，当做缓冲区使用
        BufferedWriter bw = new BufferedWriter(new FileWriter("file0.txt", true));//设置追加写模式
        for (int i = 0; i < list.size(); i++) {
            String str = list.get(i);
            bw.write(str);
            bw.newLine();
        }
        bw.close();
    }

    private static void bufferedReaderTest2() throws IOException {
        //字符缓冲输入流
        BufferedReader br = new BufferedReader(new FileReader("file0.txt"));


        //指定一次读几个字符取缓冲数据
        String len;
        System.out.println("字符缓冲输入流，读取一行");
        while ((len = br.readLine()) != null) {
            System.out.println(len);
        }
        br.close();
    }


    private static void bufferedReaderTest() throws IOException {
        //字符缓冲输入流
        BufferedReader br = new BufferedReader(new FileReader("file0.txt"));

        //不使用缓冲流时的读法
//        char[]buf=new char[1024];
//        int len=0;
//        while((len=br.read(buf))!=-1){
//            System.out.println(new String(buf,0,len));
//        }

//        使用缓冲流后的读法，单个字符取缓冲数据
//        int len;
//        System.out.println("字符缓冲输入流");
//        while((len=br.read())!=-1){
//            System.out.print((char)len);
//        }

        //指定一次读几个字符取缓冲数据
        int len;
        System.out.println("字符缓冲输入流，取多个字符");
        char[] buf = new char[512];
        while ((len = br.read(buf)) != -1) {
            System.out.println(new String(buf, 0, len));
        }
        br.close();
    }

    private static void bufferedWriterTest() throws IOException {
        //字符缓冲输出流
        //缓冲流里面有一个8192长度的char[]字符数组，当做缓冲区使用
        BufferedWriter bw = new BufferedWriter(new FileWriter("file0.txt", true));//设置追加写模式
        bw.write("hello");
        bw.newLine();//自动根据操作系统更换换行符
        bw.write("java io");
        bw.close();
    }

    private static void fileWriterTest() throws IOException {
        //字符输出流
        FileWriter fw = new FileWriter("file0.txt");
        fw.write("abc");
        fw.write("def");
        fw.close();
    }

    private static void fileReaderTest() throws IOException {
        //字符输入流
        FileReader fr = new FileReader("file0.txt");
        char[] buf = new char[1024];
        int len = 0;
        while ((len = fr.read(buf)) != -1) {
            System.out.println(new String(buf, 0, len));
        }
        fr.close();
    }

}
